package cn.simon.free.redis.jedis.sorted.set;

import cn.simon.free.redis.jedis.common.AbstractJedis;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * redis - sorted set
 *
 * @author simon
 * @date 2021/5/28 11:09
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Component
public class JedisSortedSetSupport extends AbstractJedis implements JedisSortedSet {

    @Override
    public Boolean zAdd(String key, Long score, String value) {
        return super.redisTemplate.boundZSetOps(key).add(value, score);
    }

    @Override
    public Long zCard(String key) {
        return super.redisTemplate.boundZSetOps(key).zCard();
    }

    @Override
    public Long zCount(String key, Long min, Long max) {
        return super.redisTemplate.boundZSetOps(key).count(min, max);
    }

    @Override
    public Double zInCrBy(String key, String member, Double score) {
        return super.redisTemplate.boundZSetOps(key).incrementScore(member, score);
    }

    @Override
    public List<Object> zRange(String key, Long start, Long stop) {
        return super.redisTemplate.boundZSetOps(key).range(start, stop).stream().map(serializable -> String.valueOf(serializable)).collect(Collectors.toList());
    }

    @Override
    public List<Object> zrRangeByScore(String key, Long min, Long max) {
        return super.redisTemplate.boundZSetOps(key).rangeByScore(min, max).stream().map(serializable -> String.valueOf(serializable)).collect(Collectors.toList());
    }

    @Override
    public Long zRank(String key, String member) {
        return super.redisTemplate.boundZSetOps(key).rank(member);
    }

    @Override
    public Long zRem(String key, String... member) {
        return super.redisTemplate.boundZSetOps(key).remove(member);
    }

    @Override
    public Long zRemRangeByRank(String key, Long start, Long stop) {
        return super.redisTemplate.boundZSetOps(key).removeRange(start, stop);
    }

    @Override
    public Long zRemRangeByScore(String key, Long min, Long max) {
        return super.redisTemplate.boundZSetOps(key).removeRangeByScore(min, max);
    }

    @Override
    public List<Object> zRevRange(String key, Long start, Long stop) {
        return super.redisTemplate.boundZSetOps(key).reverseRange(start, stop).stream().map(serializable -> String.valueOf(serializable)).collect(Collectors.toList());
    }

    @Override
    public List<Object> zRevRangeByScore(String key, Long min, Long max) {
        return super.redisTemplate.boundZSetOps(key).reverseRangeByScore(min, max).stream().map(serializable -> String.valueOf(serializable)).collect(Collectors.toList());
    }

    @Override
    public Long zRevRank(String key, String member) {
        return super.redisTemplate.boundZSetOps(key).reverseRank(member);
    }

    @Override
    public Double zScore(String key, String member) {
        return super.redisTemplate.boundZSetOps(key).score(member);
    }

    @Override
    public Object zScan(String key, String pattern, Long count) {
        return super.redisTemplate.boundZSetOps(key).scan(ScanOptions.scanOptions().match(pattern).count(count).build());
    }

}
